// Util.h -- Utility functions abstracted for implementation independence
// Copyright (C) 2008  Morgan Conbere
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
// USA.
//
// $Id: Util.h 18 2008-12-26 20:33:33Z mconbere $
//

#ifndef __UTIL_UTIL_H__
#define __UTIL_UTIL_H__

#include <stdint.h>

namespace util
{

// Templated Swap function to convert from Big Endian to Little Endian
template<typename T>
static T Swap(T in) { return in; }

template<>
static uint16_t Swap<uint16_t>(uint16_t in)
{
    int out = 0;
    out |= (uint16_t)((uint8_t*)(&in))[0] << 8;
    out |= ((uint8_t*)(&in))[1];
    return out;
}

template<>
static int16_t Swap<int16_t>(int16_t in)
{
    int out = 0;
    out |= (int16_t)((int8_t*)(&in))[0] << 8;
    out |= ((int8_t*)(&in))[1];
    return out;
}

} // namespace util

#endif // __UTIL_UTIL_H__
